<template>
  <section>
    <el-row style="display: flex;align-items: center;">
      <el-col :span="6">
        <el-input size="mini" placeholder="请输入关键字"  clearable v-model="searchKey" @keydown.native.prevent.enter="searchBtn" ></el-input>
      </el-col>
      <el-col :span="4">
        <i class="el-icon-search" @click="searchBtn" style="margin-left:10px" ></i>
      </el-col>
      <el-col :span="12" >
        <el-button size="mini" @click="addPropertiesBtn" style="float: right" type="primary">新建活动属性</el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-table :data="propertiesList" stripe v-loading="loading">
        <el-table-column type="index" label="序号"  width="80px"></el-table-column>
        <el-table-column prop="name" label="名称" width="150px">
        </el-table-column>
        <el-table-column prop="code" label="编码">
        </el-table-column>
        <el-table-column label="属性等级">
          <template slot-scope="scope">
            <span >{{scope.row.data.level.name}}</span>
          </template>
        </el-table-column>
        <el-table-column label="优先级">
          <template slot-scope="scope">
            {{scope.row.data.level.min}} ≤
            <span  v-show="!scope.row.edit">{{scope.row.priority}}</span>
            ≤ {{scope.row.data.level.max}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button type="primary" @click='confirm(scope.row)' size="mini">编辑</el-button>
            <el-button type="danger" @click='del(scope)' size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination v-show="totalPage>0" style="margin-top:20px;"
                     background
                     layout="prev, pager, next"
                     :page-count="totalPage"
                     @current-change="goPage">
      </el-pagination>
    </el-row>
    <el-dialog :visible.sync="editDialog" :title="currProperties.id?'编辑':'新建'" v-if="currProperties">
      <el-form :data="currProperties" label-width="80px">
        <el-form-item v-if="currProperties.id" label="id">{{currProperties.id}}</el-form-item>
        <el-form-item label="名称">
          <el-input size="mini" v-model="currProperties.name"></el-input>
        </el-form-item>
        <el-form-item label="编码">
          <el-input size="mini" v-model="currProperties.code"></el-input>
        </el-form-item>
        <el-form-item label="等级">
          <el-select v-model="currProperties.data.level" placeholder="选择等级" size="mini" value-key="id" @change="selLevelId(currProperties)">
            <el-option v-for="(level,i) in levelList" :value="level" :key="level.id" :label="level.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="优先级">
            {{currProperties.data.level.min}} ≤
            <el-input  size="mini" type="number" v-model="currProperties.priority" style="width:80px" ></el-input>
            ≤ {{currProperties.data.level.max}}
        </el-form-item>
        <el-form-item>
          <el-button size="mini" @click="subProperties(currProperties)" type="primary">完成</el-button>
          <el-button size="mini" @click="editDialog = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  export default {
    name: "properties",
    props: [''],
    data() {
      return {
        loading:false,
        searchKey:null,
        levelList:null,
        propertiesList:null,
        editDialog:false,
        currProperties:null,
        totalPage:0
      }
    },
    created(){
      this.loading=true
      this.getActivityPropertiesLevelList()
      //加载活动属性列表
      this.getActivityPropertiesList()
    },
    computed:{
    },
    watch: {
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      selLevelId(row){
        if(row.data){
          row.levelId=row.data.level.id
        }
      },
      searchBtn(){
        console.log('查询一下')
      },
      goPage(val){
        this.loading=true
        this.getActivityPropertiesList(val-1)
      },
      getActivityPropertiesList(pageNo){
        this.fetch({name:"getActivityPropertiesList",data:{page:pageNo?pageNo:0}})
          .then(res=>{
            console.log(res)
            this.loading=false
            this.propertiesList=res.content
            this.totalPage=res.totalPages
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getActivityPropertiesLevelList(){
        this.fetch({name:"getActivityPropertiesLevelList"})
          .then(res=>{
            this.levelList=res
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      addPropertiesBtn(){
        this.editDialog=true
        this.currProperties={name:'',levelId:'',priority:null,data:{level:''}}
      },
      confirm(row){
        console.log(row)
        row.priority=''+row.priority
        this.currProperties=row
        this.editDialog=true
      },
      del(scope){//删除按钮
        console.log(scope)
        return
        this.loading=true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name:"deleteActivityProperties",data:{id:scope.row.id}})
              .then(res=>{
                this.loading=false
                this.propertiesList.splice(scope.$index,1)
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      updateActivityProperties(row){
        this.fetch({name:"updateActivityProperties",data:{...row}})
          .then(res=>{
            this.loading=false
            console.log(res)
            row.edit=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      subProperties(p){
        console.log(p)
        if(p.id){
          //更新
        }else {
          //新建
          this.loading=true
          this.createActivityProperties(p)
        }
      },
      createActivityProperties(p){
        this.fetch({name:"createActivityProperties",data:p})
          .then(res=>{
            console.log(res)
            this.propertiesList.unshift(res)
            this.editDialog=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
    }
  }
</script>

<style scoped>

</style>
